package com.sharding.test.service.impl;

import com.sharding.test.dto.RPage;
import com.sharding.test.dto.TsUserDto;
import com.sharding.test.entity.TsUser;
import com.sharding.test.mapper.TsUserMapper;
import com.sharding.test.service.TsUserService;
import com.sharding.test.utils.SnowFlakeUtil;

import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangpeiyu
 * @since 2021-10-21
 */
@Slf4j
@Service
public class TsUserServiceImpl extends ServiceImpl<TsUserMapper, TsUser> implements TsUserService {
	
	
	@Autowired
	private TsUserMapper mapper;

	@Override
	public TsUser getUserById(Long id) {
		TsUser user = mapper.selectById(id);
		return user;
	}

	@Override
	public boolean create(TsUser user) {
		user.setId(SnowFlakeUtil.getUid());
		return mapper.insert(user)>0;
	}

	@Override
	public RPage<TsUserDto> getUserPage(Map<String, Object> params) {
		if(params!=null&&!params.isEmpty()) {
            if(log.isDebugEnabled()){
                log.info("params:"+params);
            }
            Long pageNo = Long.valueOf(String.valueOf(params.get("pageNo")));
            Long pageSize =Long.valueOf(String.valueOf(params.get("pageSize")));
            
            Page<TsUser> iPage = new Page<>(pageNo,pageSize);
            IPage<TsUser> epsIPage = mapper.getUserPagingMap(iPage,params);
            if(epsIPage!=null) {
            	List<TsUser> list = epsIPage.getRecords();
            	if(list!=null&&!list.isEmpty()) {
            		List<TsUserDto> dtos = new ArrayList<TsUserDto>();
            		for(TsUser ei:list) {
            			TsUserDto dto = new TsUserDto();
            			BeanUtils.copyProperties(ei, dto);
            			dtos.add(dto);
            		}
               	 return new RPage<TsUserDto>(pageNo, pageSize, 
            			 epsIPage.getPages(),epsIPage.getTotal(),dtos);
            	}
            }
		}
		return null;
	}

}
